<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "JIEQI_MODULE_NAME", "system" );
require_once( "../global.php" );
include_once( JIEQI_ROOT_PATH."/class/power.php" );
$power_handler =& jieqipowerhandler::getinstance( "JieqiPowerHandler" );
$power_handler->getsavedvars( "system" );
jieqi_checkpower( $jieqiPower['system']['adminconfig'], $jieqiUsersStatus, $jieqiUsersGroup, false, true );
jieqi_loadlang( "honors", JIEQI_MODULE_NAME );
include_once( JIEQI_ROOT_PATH."/lib/html/formloader.php" );
include_once( JIEQI_ROOT_PATH."/class/honors.php" );
$honors_handler =& jieqihonorshandler::getinstance( "JieqihonorsHandler" );
if ( empty( $_REQUEST['action'] ) )
{
		$GLOBALS['_REQUEST']['action'] = "show";
}
switch ( $_REQUEST['action'] )
{
case "new" :
		$errtext = "";
		if ( empty( $_POST['caption'] ) )
		{
				$errtext .= $jieqiLang['system']['need_honor_caption']."<br />";
		}
		if ( !is_numeric( $_POST['minscore'] ) )
		{
				$errtext .= $jieqiLang['system']['need_minscore_num']."<br />";
		}
		if ( !is_numeric( $_POST['maxscore'] ) )
		{
				$errtext .= $jieqiLang['system']['need_maxscore_num']."<br />";
		}
		$GLOBALS['_POST']['minscore'] = intval( $_POST['minscore'] );
		$GLOBALS['_POST']['maxscore'] = intval( $_POST['maxscore'] );
		if ( $_POST['maxscore'] < $_POST['minscore'] )
		{
				$errtext .= $jieqiLang['system']['max_than_min']."<br />";
		}
		if ( empty( $errtext ) )
		{
				$honors = $honors_handler->create( );
				$honors->setvar( "caption", $_POST['caption'] );
				$honors->setvar( "minscore", $_POST['minscore'] );
				$honors->setvar( "maxscore", $_POST['maxscore'] );
				$honors->setvar( "setting", "" );
				$honors->setvar( "honortype", "0" );
				if ( $honors_handler->insert( $honors ) )
				{
						break;
				}
				jieqi_printfail( $jieqiLang['system']['add_honor_failure'] );
		}
		else
		{
				jieqi_printfail( $errtext );
		}
		break;
case "delete" :
		if ( empty( $_REQUEST['id'] ) )
		{
				break;
		}
		$honors_handler->delete( $_REQUEST['id'] );
		break;
case "update" :
		if ( empty( $_REQUEST['id'] ) || empty( $_POST['caption'] ) )
		{
				break;
		}
		$honors = $honors_handler->get( $_REQUEST['id'] );
		if ( !is_object( $honors ) )
		{
				break;
		}
		$errtext = "";
		if ( empty( $_POST['caption'] ) )
		{
				$errtext .= $jieqiLang['system']['need_honor_caption']."<br />";
		}
		if ( !is_numeric( $_POST['minscore'] ) )
		{
				$errtext .= $jieqiLang['system']['need_minscore_num']."<br />";
		}
		if ( !is_numeric( $_POST['maxscore'] ) )
		{
				$errtext .= $jieqiLang['system']['need_maxscore_num']."<br />";
		}
		$GLOBALS['_POST']['minscore'] = intval( $_POST['minscore'] );
		$GLOBALS['_POST']['maxscore'] = intval( $_POST['maxscore'] );
		if ( $_POST['maxscore'] < $_POST['minscore'] )
		{
				$errtext .= $jieqiLang['system']['max_than_min']."<br />";
		}
		if ( empty( $errtext ) )
		{
				$honors->setvar( "caption", $_POST['caption'] );
				$honors->setvar( "minscore", $_POST['minscore'] );
				$honors->setvar( "maxscore", $_POST['maxscore'] );
				if ( $honors_handler->insert( $honors ) )
				{
						break;
				}
				jieqi_printfail( $jieqiLang['system']['edit_honor_failure'] );
		}
		else
		{
				jieqi_printfail( $errtext );
		}
		break;
case "edit" :
		if ( empty( $_REQUEST['id'] ) )
		{
				break;
		}
		$honors = $honors_handler->get( $_REQUEST['id'] );
		if ( !is_object( $honors ) )
		{
				break;
		}
		include_once( JIEQI_ROOT_PATH."/admin/header.php" );
		$honors_form = new jieqithemeform( $jieqiLang['system']['edit_honor'], "honorsedit", JIEQI_URL."/admin/honors.php" );
		$honors_form->addelement( new jieqiformtext( $jieqiLang['system']['table_honors_caption'], "caption", 30, 250, $honors->getvar( "caption", "e" ) ), true );
		$honors_form->addelement( new jieqiformtextarea( $jieqiLang['system']['table_honors_minscore'], "minscore", $honors->getvar( "minscore", "e" ), 5, 50 ) );
		$honors_form->addelement( new jieqiformtextarea( $jieqiLang['system']['table_honors_maxscore'], "maxscore", $honors->getvar( "maxscore", "e" ), 5, 50 ) );
		$honors_form->addelement( new jieqiformhidden( "action", "update" ) );
		$honors_form->addelement( new jieqiformhidden( "id", $_REQUEST['id'] ) );
		$honors_form->addelement( new jieqiformbutton( "&nbsp;", "submit", LANG_SAVE, "submit" ) );
		$jieqiTpl->assign( "jieqi_contents", "<br />".$honors_form->render( JIEQI_FORM_MIDDLE )."<br />" );
		include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
		exit( );
}
include_once( JIEQI_ROOT_PATH."/admin/header.php" );
$criteria = new criteriacompo( );
$criteria->setsort( "minscore" );
$criteria->setorder( "ASC" );
$honors_handler->queryobjects( $criteria );
$honors = array( );
$honorary = array( );
$i = 0;
while ( $v = $honors_handler->getobject( ) )
{
		$nameary = explode( " ", $v->getvar( "caption" ) );
		$honorary[$v->getvar( "honorid" )] = array(
				"caption" => $nameary[0],
				"name" => $nameary,
				"minscore" => $v->getvar( "minscore" ),
				"maxscore" => $v->getvar( "maxscore" )
		);
		$honors[$i]['honorid'] = $v->getvar( "honorid" );
		$honors[$i]['caption'] = implode( "<br />", $nameary );
		$honors[$i]['minscore'] = $v->getvar( "minscore" );
		$honors[$i]['maxscore'] = $v->getvar( "maxscore" );
		$honors[$i]['honortype'] = $v->getvar( "honortype" );
		++$i;
}
$jieqiTpl->assign_by_ref( "honors", $honors );
$honors_form = new jieqithemeform( $jieqiLang['system']['add_honor'], "honorsnew", JIEQI_URL."/admin/honors.php" );
$honors_form->addelement( new jieqiformtext( $jieqiLang['system']['table_honors_caption'], "caption", 30, 250, "" ), true );
$honors_form->addelement( new jieqiformtext( $jieqiLang['system']['table_honors_minscore'], "minscore", 30, 50, "" ), true );
$honors_form->addelement( new jieqiformtext( $jieqiLang['system']['table_honors_maxscore'], "maxscore", 30, 50, "" ), true );
$honors_form->addelement( new jieqiformhidden( "action", "new" ) );
$honors_form->addelement( new jieqiformbutton( "&nbsp;", "submit", $jieqiLang['system']['add_honor'], "submit" ) );
$jieqiTpl->assign( "form_addhonor", "<br />".$honors_form->render( JIEQI_FORM_MIDDLE )."<br />" );
$jieqiTpl->setcaching( 0 );
$jieqiTset['jieqi_contents_template'] = JIEQI_ROOT_PATH."/templates/admin/honors.html";
include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
if ( ( !empty( $_REQUEST['id'] ) && !empty( $_POST['caption'] ) ) || 0 < count( $honorary ) )
{
		jieqi_setconfigs( "honors", "jieqiHonors", $honorary, "system" );
}
?>
